<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Rencana extends CI_Controller {
	
        public function __construct()
	{
		parent::__construct();
                $this->load->model('rencana_model','mr');
                $this->load->model('boq_model','mb');
                $this->load->model('station_model','ms');
                if(!$this->session->userdata('logged_in'))
                {
                  redirect('login');
                }
	}
	
	function rencanasList($id="")
	{
		if($id != "")
		{
			$selectedRencana = $this->mr->get($id);
		}
		else
		{
			$selectedRencana = NULL;
		}
		$data['selectedRencana'] = $selectedRencana;
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['rencanas'] = $this->mr->get();
		$data['boqs'] = $this->mb->get();
		$data['stations'] = $this->ms->get();
		$this->load->view('rencanas/index', $data);
	}
	
	function saveRencana()
	{
		$this->form_validation->set_rules('pay_item','Pay Item','required');
		$this->form_validation->set_rules('tanggal','Tanggal','required');
		$this->form_validation->set_rules('station','Staion','required');
		$this->form_validation->set_rules('jumlah','Jumlah','required');
		
                $this->form_validation->set_message('required','Kolom %s harus diisi dengan benar');
                
                
                if ($this->form_validation->run())
                {
			$id =$this->input->post('id');
			$rencanaData = array(
				'pay_item' => $this->input->post('pay_item'),
				'tanggal' => $this->input->post('tanggal'),
				'id_station' => $this->input->post('station'),
				'jumlah' => $this->input->post('jumlah')
				);
			if($this->mr->is_rencana_exist($id) == true)
			{
				//Update
				$this->mr->update($id, $rencanaData);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$rencanaData['rencana'].'</b> berhasil di ubah!</p>');
				redirect('rencana/rencanasList');
			}
			else
			{
				//Add
				$this->mr->insert($rencanaData);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$rencanaData['rencana'].'</b> berhasil di tambah!</p>');
				redirect('rencana/rencanasList');
			}
                }
                else
                {
                        $this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>Isi form dengan benar!</p>');
			redirect('rencana/rencanasList');
                }        
	}
	
	function deleteRencana($id="")
	{
		if($this->mr->is_rencana_exist($id) == true)
		{
			$this->mr->delete($id);
			$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$pay_item.'</b> berhasil di hapus!</p>');
			redirect('rencana/rencanasList');
		}
		else
		{
			$this->session->set_flashdata('msg', '<p class="alert alert-danger"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>Tidak bisa hapus diri sendiri!</b></p>');
			redirect('rencana/rencanasList');
		}
	}
}